<?php
namespace app\controller;

use app\BaseController;
use Workerman\Worker;
class Index extends BaseController
{
    private $server;
    private function start()
    {
        // 创建一个Worker监听20002端口，不使用任何应用层协议
        // $this->server = new Worker("tcp://0.0.0.0:20002");
        // // 启动4个进程对外提供服务
        // $this->server->count = 4;
        // // $handler = \App::make('handlers\WorkermanHandler');
        // // 连接时回调
        // $this->server->onConnect = [$handler, 'onConnect'];
        // // 收到客户端信息时回调
        // $this->server->onMessage = [$handler, 'onMessage'];
        // // 进程启动后的回调
        // $this->server->onWorkerStart = [$handler, 'onWorkerStart'];
        // // 断开时触发的回调
        // $this->server->onClose = [$handler, 'onClose'];
        // // 运行worker
        Worker::runAll();
    }
    
    public function index()
    {
        echo 11;
        event('TestSub','成功');die;
    }

    public function hello()
    {
        return 'hello,' ;
    }
    public function login(){
    	echo '登录';
    	event('UserLogin');
    }
    public function logout(){
    	echo '退出';
    	event('UserLogout');
    }
    public function workerman()
    {
        // echo 1;die;
        // return $this->fetch();
        //创建一个Worker监听2346端口，使用websocket协议通讯
        $ws_worker = new Worker("websocket://0.0.0.0:2346");
        // 启动4个进程对外提供服务
        $ws_worker->count = 4;

        // 当收到客户端发来的数据后返回hello $data给客户端
        $ws_worker->onMessage = function($connection, $data)
        {
            // echo $connection->id;
            // 向客户端发送hello $data
            $connection->send('hello   低调低调哈哈哈' . $connection->id);
        };

        // 运行worker
        $aa = Worker::runAll();
        // halt($aa);

    }
}
